package cn.jishiyu11.xeqb.activity.personalCenterActivitys

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import android.widget.TextView
import cn.jishiyu11.xeqb.EntityClass.FragmengData.DiscoverBean
import cn.jishiyu11.xeqb.R
import cn.jishiyu11.xeqb.adapter.DiscoverRecyclerAdapter
import cn.jishiyu11.xeqb.base.BaseActivity
import cn.jishiyu11.xeqb.http.http.i.DataCallBack
import cn.jishiyu11.xeqb.http.http.i.httpbase.DISCOVER_URL
import cn.jishiyu11.xeqb.http.http.i.httpbase.OkHttpManager
import cn.jishiyu11.xeqb.utils.showShortToast
import com.google.gson.Gson
import com.umeng.analytics.MobclickAgent
import kotlinx.android.synthetic.main.act_onlyrecyclerview_layout.*
import kotlinx.android.synthetic.main.tab_activity_fra.*
import okhttp3.Request
import java.io.IOException

/**
 * Created by jsy_zj on 2018-03-12
 * 收款记录
 */
class GatheringHistoryActivity :BaseActivity(),DataCallBack {

    var DiscList:MutableList<DiscoverBean.Discover>?=null
    var mAdapter: DiscoverRecyclerAdapter? = null
    var discBean: DiscoverBean? = null
    var mIsRefresh = false


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_onlyrecyclerview_layout)
        findViewById()
    }


    override fun findViewById() {
        title_image.visibility = View.VISIBLE
        title_image.setOnClickListener{finish()}
        (title_view as TextView).text = this.getString(R.string.personalcenter_gathering_history)

        act_onlyrecyclerview_layout_recyclerv.layoutManager = LinearLayoutManager(this)
        DiscList = ArrayList<DiscoverBean.Discover>()
        mAdapter = DiscoverRecyclerAdapter(this!!,DiscList!!)
        act_onlyrecyclerview_layout_recyclerv.adapter = mAdapter

        act_onlyrecyclerview_layout_recyclerv.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView?, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                var layoutManager: LinearLayoutManager = recyclerView?.layoutManager as LinearLayoutManager
                var lastPosition = layoutManager.findLastVisibleItemPosition()
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastPosition == DiscList!!.size-1){
                    if (DiscList?.size!=0){

                        getData()

                    }

                }
            }
        })

        act_onlyrecyclerview_layout_refreshlayout.setOnRefreshListener {
            if (!mIsRefresh){
                mIsRefresh = true
                getData()
            }
        }
        getData()

    }

    override fun initView() {
    }


    /**
     * recycler 数据刷新
     * */
    private fun setRecyclerData(result: String) {
        if (mIsRefresh){
            mIsRefresh = false
            act_onlyrecyclerview_layout_refreshlayout.isRefreshing =false
        }
        if (DiscList!!.size>0){
            DiscList!!.clear()
        }

        if (!result.isNullOrEmpty()){
            discBean = Gson().fromJson(result, DiscoverBean::class.java)
            var msgList = discBean?.discover
            if (msgList!=null){
                DiscList!!.addAll(msgList!!)
            }
            mAdapter?.notifyDataSetChanged()
        }
    }

    /**
     * 加载更多
     * */
    private fun addMoreData(result: String) {
        if (mIsRefresh){
            mIsRefresh = false
            act_onlyrecyclerview_layout_refreshlayout.isRefreshing =false
        }
        if (!result.isNullOrEmpty()){
            discBean = Gson().fromJson(result, DiscoverBean::class.java)
            var msgList = discBean?.discover
            if (msgList!=null){
                DiscList!!.addAll(msgList!!)
            }
            mAdapter?.notifyDataSetChanged()
        }
    }


    /**
     * 获取数据
     * */
    private fun getData(){
        OkHttpManager.postAsync(DISCOVER_URL,"GATHERING_URL",null,this)
    }

    override fun requestFailure(request: Request, name: String, e: IOException) {
        this.showShortToast("数据获取失败！")
    }

    override fun requestSuccess(result: String, name: String) {
        when(name){
            "GATHERING_URL" ->{
                if (mIsRefresh){
                    setRecyclerData(result)
                }else{
                    addMoreData(result)
                }
            }
            else ->{}
        }
    }


    override fun onResume() {
        super.onResume()
        MobclickAgent.onResume(this)
    }

    override fun onPause() {
        super.onPause()
        MobclickAgent.onPause(this)
    }



}